package cn.gok.dao;

import cn.gok.tool.Jdbc;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SqlDao {
    //建立与数据库的连接
    private Connection conn;
    //创建信差
    private PreparedStatement pstm;
    //创界结果集
    private ResultSet rs;

    public List link(Jdbc userjdbc){
        String stuSQL="show TABLES";
        List<String> linkres=new ArrayList<String>();
        try {
            conn= userjdbc.getDBConnection();
            pstm=conn.prepareStatement(stuSQL);
            rs=pstm.executeQuery();
           while (rs.next()){
               linkres.add(rs.getString(1));
           }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }finally {
            userjdbc.close(conn, pstm, rs);
        }
        return linkres;
    }
//    查询语句调用方法
    public List sel(Jdbc userjdbc, String sql){
        List list=new ArrayList();
        try {
            conn= userjdbc.getDBConnection();
            pstm=conn.prepareStatement(sql);
            rs=pstm.executeQuery();
            ResultSetMetaData metaData=rs.getMetaData();
            while (rs.next()){
                for (int i = 0; i < metaData.getColumnCount(); i++){
                    list.add(rs.getString(metaData.getColumnName(i+1)));
                }

            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }finally {
            userjdbc.close(conn, pstm, rs);
        }
        return list;
    }
}
